package com.springboot_rest.controller;


import com.springboot_rest.entity.User;
import com.springboot_rest.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author MagicMushroom
 * @date 2021/6/29
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @GetMapping
    public List<User> findAll(Model model){
        List<User> all = userService.findAll();
        model.addAttribute("userList",all);
        return all;
    }

    @PostMapping()
    public void addOne( User user){
        userService.insertOne(user);
    }

    @GetMapping("/{id}")
    public User findOne(@PathVariable("id")Integer id){
        return userService.findOne(id);
    }

    @PatchMapping
    public void update(@RequestBody User user){
        userService.update(user);
    }

    @DeleteMapping("/{id}")
    public void deleteOne(@PathVariable("id")Integer id){
        userService.deleteOne(id);
    }

}
